package com.qust.edu.aliyun.service.impl;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qust.edu.aliyun.dto.UserDto;
import com.qust.edu.aliyun.mapper.UserMapper;
import com.qust.edu.aliyun.pojo.User;
import com.qust.edu.aliyun.service.UserService;
import com.qust.edu.aliyun.utils.StringUtilss;
import com.qust.edu.aliyun.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * projectName: aliyun_guanli
 *
 * @author: 姜龙
 * time: 2021/9/28 10:58
 * description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 登录
     * @param userDto
     * @return
     */
    @Override
    public R login(UserDto userDto) {
        //判断账号和密码是否为空
        if (StringUtils.isEmpty(userDto.getUser_password())|| StringUtils.isEmpty(userDto.getUser_password()))  {
            return R.FAIL("手机号或者密码为null!");
        }

        //密码加密处理
        String new_pwd= DigestUtils.md5DigestAsHex(userDto.getUser_password().getBytes());

        // 账号和密码进行数据库验证
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone",userDto.getUser_phone());
        queryWrapper.eq("user_password",new_pwd);

        //根据账号和密码查询
        User users = userMapper.selectOne(queryWrapper);

        //返回结果
        if (users==null){
            return R.FAIL("手机号或者密码错误！");

        }
        //判断管理员是否被删
        if (users.getDeleted() == 1){
            return R.FAIL("用户已被删除，请联系总管理员恢复！");
        }

        return R.OK("登陆成功",users);
    }

    /**
     * 检验是否注册
     * @param userDto
     * @return
     */
    @Override
    public R findUserName(UserDto userDto) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone",userDto.getUser_phone());

        List<User> users = userMapper.selectList(queryWrapper);
        if (users!=null&&users.size()>0){
            return R.FAIL("账号已存在！，请更换！");
        }

        return R.OK("账号可以注册");

    }

    /**
     * 注册
     * @param userDto
     * @return
     */

    @Override
    public R register(UserDto userDto) {
        //判断账号和密码是否为空
        if (StringUtils.isEmpty(userDto.getUser_phone())|| StringUtils.isEmpty(userDto.getUser_password()))  {
            return R.FAIL("账号或者密码为null!");
        }

        //密码加密处理
        String new_pwd= DigestUtils.md5DigestAsHex(userDto.getUser_password().getBytes());

        //注册即可
        User user = new User();
        user.setUserPhone(userDto.getUser_phone());
        user.setUserPassword(new_pwd);
        user.setUserName(userDto.getUserName());
        if (userDto.getUserName()==null||userDto.getUserName()==""){
            user.setUserName("无名氏");
        }
        int rows=userMapper.insert(user);
        //返回结果
        if (rows==0){
            return R.FAIL("注册用户失败！");

        }

        return R.OK("注册用户成功！");

    }

    /**
     * 添加
     *
     * @param user
     * @return
     */
    @Override
    public R getAddManage(User user) {

        //密码加密处理
        user.setUserPassword(StringUtilss.dealPassword(user.getUserPassword()));
        //判断用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone",user.getUserPhone());
        User dbUser = userMapper.selectOne(queryWrapper);
        if (dbUser !=null){
            return R.FAIL("此用户已存在，请勿重复添加！");
        }else {
            int rows =0;
            try {
                rows = userMapper.insert(user);
            }catch (Exception e){
                System.out.println("e = " + e);
            }
            if (rows == 0){
                return R.FAIL("数据异常，插入失败！");
            }


            return R.OK("插入成功");
        }
    }

    /**
     * 删除
     *
     * @param userDto
     * @return
     */
    @Override
    public R getDeleteManage(UserDto userDto) {

        User user = new User();
        user.setDeleted(1);
        user.setUserId(userDto.getUserId());
        int rows = 0;
        try {
            rows = userMapper.updateById(user);
        }catch (Exception e){

        }

        if (rows == 0){
            return R.FAIL("删除失败！");
        }


        return R.OK("删除成功");
    }

    /**
     * @param user
     * @return
     */
    @Override
    public R getModifyManage(User user) {

        user.setUserPassword(StringUtilss.dealPassword(user.getUserPassword()));
        int rows = 0;
        try {
            rows = userMapper.updateById(user);
        }catch (Exception e){

        }
        if (rows == 0){
            return R.FAIL("修改失败！");
        }


        return R.OK("修改成功");
    }

    /**
     * 查询所有管理员
     *
     * @return
     */
    @Override
    public R findAllUsers() {

        List<User> users = userMapper.selectList(null);

        return R.OK("查询成功！",users);
    }
}
